Create an Editor with a Dynamic Dropdown Box

Description

You can use an editor to create a dynamic drop down box. The dropdown box can be called by multiple fields inside a form view, such as a 'State' and a 'City' field. It can then use javascript to filter the values of the one field based on the value of the other.

Create a Simple List Control

  1. In the UX Builder on the UX Controls page, open the 'Data Controls' menu. Click on the [List] option to add a list control to the component.

    images/dd2.png
  2. Highlight the list control in the controls tree. In the properties list on the right click on the 'List properties' property in the 'List Properties' section. The list builder will open.

    images/dd3.png
  3. On the list builder's 'Data Source' pane set the 'Data Source Type' to 'Static'.

    images/dd4.png
  4. Click the [...] button next to the 'Static data' property.

    images/dd5.png
  5. Click the 'Sample data' hyperlink in the lower left-hand corner of the 'Static Choices' dialog .

    images/dd6.png
  6. Select the 'Name and addresses' data set from among the 'Sample Data' options and click OK and OK again.

    images/dd7.png
  7. Open the 'List Layout' pane. Highlight the 'Firstname', 'Lastname', 'City', and 'State' fields in the 'Available Fields' list. Use the blue > arrow icon to move the fields to the 'Columns in List' section. Click OK to close the list builder.

    images/dd8.png

Define JSON data to Filter

  1. In the UX Builder's main menu, open the 'Events' section and then the 'Client-side' page.

    images/dd9.png
  2. In the Client-Side Events list highlight the 'onRenderComplete' event. To quickly locate this event type 'render' into the filter underneath the list.

    images/dd10.png
  3. Add the following JSON to the 'onRenderComplete' event definition.

    {dialog.object}._cities = [
      {
         "City": "Boston",
         "State": "MA"
      },
      {
         "City": "Cambridge",
         "State": "MA"
      },   
      {
         "City": "New York",
         "State": "NY"
      },   
      {
         "City": "Los Angeles",
         "State": "CA"
      },   
      {
         "City": "Chicago",
         "State": "IL"
      },   
      {
         "City": "Springfield",
         "State": "IL"
      },   
      {
         "City": "Boulder",
         "State": "CO"
      },   
      {
         "City": "Ithaca",
         "State": "NY"
      }
    ];
    images/dd11.png

Define Javascript Functions

  1. In the UX Builders main menu open the 'Code' section and click on the 'Javascript functions' page to open it. Define a getStates() function and getCities function using the following code:

    function getStates() {
       var _d = {dialog.object}._cities;
       var s = [];
       for(var i = 0; i < _d.length; i++) {
          if(s.indexOf(_d[i].State) < 0) {
             s.push(_d[i].State);
          }
       }
    
       s = s.sort();
    
       return s; 
    };
    
    function getCities(state) {
        var _d = {dialog.object}._cities;
        var c = [];
        for(var i = 0; i < _d.length; i++) {
           if(_d[i].State == state) {
               if(c.indexOf(_d[i].City) < 0) {
                   c.push(_d[i].City);
               }
           }
        }
        c = c.sort();
        return c;
    
    };
    images/dd12.png
    The two functions process the client-side data in this component. The getStates() function takes the data created in the client-side onInitialize event. It then loops through all of the records in the data and creates an array of unique states. In other words, if the state has already has been pushed onto the array then it isn't pushed a second time. At the end of the function this array of unique states is sorted. The array of states is then returned. The second function, getCities(), takes a 'state' as its argument. The function will then push this data onto a cities array, but only if the state for the current entry in the array matches the state value that is passed into the function.

Create Two Editors in an EditorSet

  1. Open the UX Controls page again and open the 'Containers' menu. Click on the [Container] option.

    images/dd13.png
  2. From the 'Container Type' list select the 'EditorSet' option and click 'Insert After'.

    images/dd14.png
  3. In the controls tree, highlight the opening tag of the EditorSet container. Open the 'Defined Controls' menu and select the 'Editor-TextBox' option. An textbox editor should be added inside the EditorSet.

    images/dd15.png
  4. Highlight the ending tag for the Editor-TextBox container,"Editor End:EDITOR_1]". In the Defined Controls menu click on the 'Editor-TextBox' option a second time

    images/dd16.png
  5. In the controls tree highlight the [TextBox] control inside the second editor that you just defined.

    images/dd17.png
  6. In the properties list on the right click the [...] button next to the 'Control type' property.

    images/dd18.png
  7. In the 'Select Control Type' dialog choose 'DropDownBox' from the list and click OK.

    images/dd19.png
  8. Highlight the opening tag of the second editor container, "[Editor: EDITOR_2]"

    images/dd20.png
  9. Click the [...] button next to the "Set value in editor" property.

    images/dd21.png
  10. Add the following code to the 'Set value in editor' definition.

    if(settings['fieldName'] == 'State') {
         var choices = getStates();
    } 
    else if(settings['fieldName'] == 'City') {
        var state = settings.formView.data.State;
         choices = getCities(state);
    }
    if(typeof choices != 'undefined') {
        {dialog.object}.populateDropdownBox('EDITOR_2_TXTBOX',choices,true);
    }
    
    {dialog.object}.setValue('EDITOR_2_TXTBOX',value);
    images/dd22.png
    If we are currently setting the value for the state field and that is equal to state then the javascript function called getStates() is called. This provides an array of choices for the dropdown box. If the City field is being populated then it is necessary to filter the list of cities based on the current value in the state field. We can get this information using 'settings.formView.data.State'. The choices are obtained from the getCities() function. Finally the dropdownbox is populated using the populateDropdownBox() function. Note that in the video example the state variable is set to 'settings.data.State'. Due to changes in Alpha Anywhere this should now 'var state = settings.formView.data.State;'.

Create the Form View Control

  1. On the UX Controls page highlight the list control in the controls tree and open the 'Other Controls' menu. Select the [Form View] option to add a form view control under the list control.

    images/dd23.png
  2. Highlight the form view control in the controls tree. In the properties list click on the [...] button next to the 'Form properties' property.

    images/dd24.png
  3. On the Form Builder's 'Data Source' pane. Click the dropdown button next to the 'Datasource type' property. Select 'List'.

    images/dd25.png
  4. Click the [...] button next to the 'List name' property. Select the list that was defined in the first section of this guide, "list 1".

    images/dd26.png
  5. Open the fields pane. Highlight the Firstname field. In the properties list on the right click the [...] button next to the 'Editor set' property.

    images/dd27.png
  6. In the 'Editor Set' list select 'EDITORSET_1'. In the 'Editor' list select EDITOR_1, the textbox editor. Click the 'Assign selection to multiple fields' hyperlink.

    images/dd28.png
  7. Highlight the 'Firstname' and 'Lastname' fields in the 'Select Multiple Fields' dialog and click OK.

    images/dd29.png
  8. Highlight the 'City' field. Click the [...] button next to the 'Editor set' property.

    images/dd30.png
  9. Set the 'Editor Set' to be 'EDITORSET_1' and the 'Editor' to EDITOR_2. Click the 'Assign selection to multiple fields' hyperlink.

    images/dd31.png
  10. Highlight the 'City' and the 'State' fields and click OK.

    images/dd32.png
  11. Click the [...] button next to the 'User defined editor and template settings' property in the Editor Settings section.

    images/dd33.png
  12. Click the 'Add setting' property button at the bottom of the User Defined Editor and Template Settings: Field - 'City' dialog

    images/dd34.png
  13. In the Setting dialog make the 'Setting name' to read 'fieldName'. Set the 'Value' to read 'City'. Click OK and OK again.

    images/dd35.png
  14. Highlight the 'State' field in the 'Fields in Data Source' list.

    images/dd36.png
  15. Click the [...] button next to the 'User defined editor and template settings' property in the Editor Settings section.

    images/dd37.png
  16. Click the 'Add setting' button again.

    images/dd38.png
  17. Give the setting the 'Setting name' of 'fieldName'. Set the value to read 'State'. Click OK and OK again.

    images/dd39.png
  18. Open the 'Form Layout' pane. Click the 'Add item' button at the bottom of the pane.

    images/dd40.png
  19. In the 'Select Control Type' dialog highlight the 'Data Controls' option in the 'Category' list, the [Label] option in the 'Control' list, and then highlight all of the fields on the right. Click OK and OK again to close the Form View Builder.

    images/dd41.png
  20. Run the component in Live Preview. Click on the 'State' field and select a state from the dropdown.

    images/dd42.png
  21. Save the selected state. Click the 'City' field in the FormView. You should see a dropdown that only displays cities in the selected State.

    images/dd43.png

See Also